<?php

/**
 * 绩效
 * File Name：PerformanceController.php
 * File Encoding：UTF-8
 * File New Time：2014-7-25 15:05:46
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class PerformanceController extends AdminBase {

    public function actionIndex() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('index', $_POST));
            exit;
        }
        $where = array(
            'order' => 'id desc',
        );
        $admin_id = (int) $_GET['admin_id'];
        if ($admin_id) {
            $where['admin_id'] = $admin_id;
            $this->assign('admin_id', $admin_id);
        }
        $keyword = trim($_GET['keyword']);
        if (!empty($keyword)) {
            $this->assign('keyword', $keyword);
            $type = (int) $_GET['type'];
            $this->assign('type', $type);
            switch ($type) {
                case 1:
                    $where['product_id'] = $keyword;
                    break;
                case 2:
                    //$where['product_id'] = $keyword;
                    break;
                case 3:
                    $where['nick'] = array('LIKE', "%{$keyword}%");
                    break;
                case 4:
                    $where['orders_number'] = $keyword;
                    break;
            }
        }
        $model = SalesDataModel::model();
        $this->basePage($model, $where);
        $this->render();
    }
    //删除指定日期数据
    public function actionDelete(){
        if(IS_POST){
            $model = SalesDataModel::model();      
           if($model->deleteAsTime()){
               $this->success('删除成功!');
           }
           else{
               $this->error($model->getOneError()?$model->getOneError():'删除失败!');
           }
        }       
    }

    //导入
    public function actionImport() {
        $model = SalesDataModel::model();
        if (IS_POST) {
            if (empty($_FILES['file']['name'])) {
                $this->error("请选择上传文件！");
            }
            //上传临时文件地址
            $filename = $_FILES['file']['tmp_name'];
            if (strtolower(substr($_FILES['file']['name'], -3, 3)) != 'csv') {
                $this->error("上传的文件格式有误！");
            }
            if ($model->excelImport($filename)) {
                $source = (int) $_POST['source'];
                $this->success('数据读取成功，开始导入数据！', self::U('import', array('act' => 'import', 'source' => $source)));
            } else {
                $this->error('数据导入失败！');
            }
        } else {
            $action = $_GET['act'];
            $count = (int) $_GET['count'];
            if ($action == 'import') {
                $source = (int) Yii::app()->request->getParam('source');
                $key = 'SalesDataModel_excelImport';
                $memcache = new Memcache();
                $memcache->connect('127.0.0.1', 11211);
                $dataList = $memcache->get($key);
                if (empty($dataList)) {
                    $this->success('导入完成1！', self::U('import'));
                    exit;
                }
                $sum = $_GET['sum'] ? $_GET['sum'] : count($dataList);
                $i = 0;
                foreach ($dataList as $k => $rs) {
                    $model->import($rs, $source);
                    unset($dataList[$k]);
                    $i++;
                    $count++;
                    if ($i >= 100) {
                        if (empty($dataList)) {
                            $memcache->delete($key);
                        } else {
                            $memcache->set($key, $dataList, 0, 600);
                        }
                        $this->success("数据导入中....，已导入：{$count}，总数：{$sum}", self::U('import', array('count' => $count, 'act' => 'import', 'sum' => $sum, 'source' => $source)));
                        exit;
                    }
                }
                $this->success('导入完成2！', self::U('import'));
            } else {
                $this->render();
            }
        }
    }

    //统计
    public function actionAnalysis() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('analysis', $_POST));
            exit;
        }
        $where = array();
        $admin_id = (int) $_GET['admin_id'];
        if ($admin_id) {
            $where['admin_id'] = $admin_id;
            $this->assign('admin_id', $admin_id);
        }
        //栏目
        $cat_id = $_GET['cat_id'];
        if (!empty($cat_id)) {
            $where['cat_id'] = (int) $cat_id;
            $this->assign('cat_id', $cat_id);
        }
        $keyword = trim($_GET['keyword']);
        if (!empty($keyword)) {
            $this->assign('keyword', $keyword);
            $type = (int) $_GET['type'];
            $this->assign('type', $type);
            switch ($type) {
                case 1:
                    $where['product_id'] = $keyword;
                    break;
            }
        }
        // 上架时间
        if (isset($_GET['start_time']) && $_GET['start_time']) {
            $start_time = strtotime($_GET['start_time']);
            $this->assign('start_time', $_GET['start_time']);
            if ($_GET['end_time']) {
                $end_time = strtotime($_GET['end_time']) + 86399;
                $this->assign('end_time', $_GET['end_time']);
            }
            if ($start_time) {
                $where['start_time'] = array('ELT', $start_time);
            }
            if ($end_time > $start_time) {
                $where['start_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'and');
            }
        }
        //统计时间
        if (isset($_GET['start_time2']) && $_GET['start_time2']) {
            $start_time2 = strtotime($_GET['start_time2']);
            $end_time2 = strtotime($_GET['end_time2']);
            if ($_GET['end_time2']) {
                $end_time2 = $end_time2 + 86399;
            }
            if ($start_time2) {
                $where['logtime'] = array('ELT', $start_time2);
                $this->assign('start_time2', $_GET['start_time2']);
            }
            if ($end_time2 > $start_time2) {
                $where['logtime'] = array(array('EGT', $start_time2), array('ELT', $end_time2), 'and');
                $this->assign('end_time2', $_GET['end_time2']);
            }
        }

        $model = GuestFenxiModel::model();
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $order = (int) $_GET['order'];
        if ($order) {
            $criteria->order = 'turnover desc,id desc';
        } else {
            $criteria->order = 'view desc,id desc';
        }
        $data = $model->findAll($criteria);
        //销量总数
        $criteria->select = 'sum(turnover) as turnover';
        $dangSum = $model->find($criteria)->turnover;
        $criteria->select = 'sum(sales) as sales';
        $zongSales = $model->find($criteria)->sales;

        $this->assign('order', $order);
        $this->assign('data', $data ? $data : array());
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->assign('dangSum', $dangSum);
        $this->assign('zongSales', number_format($zongSales, 2));
        $this->render();
    }

    //成交数重新计算
    public function actionReset_turnover() {
        //条件
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $reset_time = trim($_POST['reset_time']);
            if (!empty($reset_time)) {
                $reset_time = strtotime($reset_time);
            } else {
                $this->error('请指定时间！');
            }
            $_POST['start_time'] = strtotime(date('Y-m-d 00:00:00', $reset_time));
            $_POST['end_time'] = strtotime(date('Y-m-d 23:59:59', $reset_time));
            $this->redirect(self::U('reset_turnover', $_POST));
            exit;
        }
        $goods = GoodsModel::model();
        $start_time = $_GET['start_time'];
        $end_time = $_GET['end_time'];
        $goods_id = $_GET['goods_id'];
        $where = array(
            'order' => 'goods_id asc',
            //'status' => 1,
            'start_time' => array('LT', $end_time),
            'end_time' => array('EGT', $end_time),
            'limit' => 20,
        );
        if ($goods_id) {
            $where['goods_id'] = array('GT', $goods_id);
        }
        $data = GoodsModel::model()->findAll($goods->where($where));
        if (empty($data)) {
            $this->success('执行完成！', self::U('analysis'));
            exit;
        }
        $GuestFenxiModel = GuestFenxiModel::model();
        $guest = GuestDataModel::model();
        foreach ($data as $rs) {
            $goods_id = $rs->goods_id;
            if (empty($rs)) {
                continue;
            }
            //官网成交数
            $guan_turnover = (int) $guest->getTurnoverGuan($rs->goods_id, $start_time, $end_time);
            //优站成交数
            $you_turnover = (int) $guest->getTurnoverYou($rs->goods_id, $start_time, $end_time);
            //总成交数
            $turnover = $guan_turnover + $you_turnover;
            //当日成交额 成交数*每单金额
            $sales = $rs->promo_price * $turnover;
            $info = $GuestFenxiModel->find($GuestFenxiModel->where(array(
                        'goods_id' => $rs->goods_id,
                        'product_id' => $rs->product_id,
                        'years' => (int) date('Y', $start_time),
                        'month' => (int) date('m', $start_time),
                        'day' => (int) date('d', $start_time),
            )));
            if (!empty($info)) {
                $info->turnover = $turnover;
                $info->guan_turnover = $guan_turnover;
                $info->you_turnover = $you_turnover;
                $info->sales = $sales;
                $info->save();
            }
        }
        $this->assign('waitSecond', 100);
        $this->success('继续执行下一轮~', self::U('reset_turnover', array(
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'goods_id' => $goods_id,
        )));
    }

    //来路统计
    public function actionReferer() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('referer', $_POST));
            exit;
        }
        $model = GuestRefererModel::model();
        $where = array(
            'order' => 'id desc',
        );
        $keyword = trim($_GET['keyword']);
        if (!empty($keyword)) {
            $this->assign('keyword', $keyword);
            $type = (int) $_GET['type'];
            $this->assign('type', $type);
            switch ($type) {
                case 1:
                    $domain = $model->getDomain($keyword);
                    $where['root_domain'] = $domain['root_domain'];
                    break;
                case 2:
                    $domain = $model->getDomain($keyword);
                    $where['domain'] = $domain['domain'];
                    break;
            }
            $where['order'] = 'frequency desc';
        }
        // 时间
        if (isset($_GET['start_time']) && $_GET['start_time']) {
            $start_time = strtotime($_GET['start_time']);
            $this->assign('start_time', $_GET['start_time']);
            if ($_GET['end_time']) {
                $end_time = strtotime($_GET['end_time']) + 86399;
                $this->assign('end_time', $_GET['end_time']);
            }
            if ($start_time) {
                $where['created_time'] = array('ELT', $start_time);
            }
            if ($end_time > $start_time) {
                $where['created_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'and');
            }
            $where['order'] = 'frequency desc';
        }
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);

        $this->assign('data', $data ? $data : array());
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->render();
    }

    //广告
    public function actionAd() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U('ad', $_POST));
            exit;
        }
        $model = GuestAdModel::model();
        $where = array(
            'order' => 'id desc',
        );
        // 时间
        if (isset($_GET['start_time']) && $_GET['start_time']) {
            $start_time = strtotime($_GET['start_time']);
            $this->assign('start_time', $_GET['start_time']);
            if ($_GET['end_time']) {
                $end_time = strtotime($_GET['end_time']) + 86399;
                $this->assign('end_time', $_GET['end_time']);
            }
            if ($start_time) {
                $where['created_time'] = array('ELT', $start_time);
            }
            if ($end_time > $start_time) {
                $where['created_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'and');
            }
            $where['order'] = 'view desc';
        }
        $keyword = trim($_GET['keyword']);
        if (!empty($keyword)) {
            $this->assign('keyword', $keyword);
            $where['adname'] = array('LIKE', "%{$keyword}%");
            $where['order'] = 'view desc';
        }

        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);

        $this->assign('data', $data ? $data : array());
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->render();
    }

    //广告标识管理
    public function actionAdlist() {
        $model = GuestAdlistModel::model();
        //是否修改
        $save = Yii::app()->request->getParam('save');
        if ($save) {
            $data = $model->find($model->where(array('admark' => $_POST['admark'])));
            if (empty($data)) {
                $this->error('该广告标识不存在！');
            }
            if (empty($_POST['adname'])) {
                unset($_POST['adname']);
            }
            $data->attributes = $_POST;
            if ($data->validate() && $data->save()) {
                $this->success('修改成功！', self::U('ad'));
                exit;
            } else {
                $this->error($data->getOneError() ? $data->getOneError() : '修改失败！');
            }
        } else {
            $model->attributes = $_POST;
            $model->setIsNewRecord(true);
            if ($model->validate() && $model->save()) {
                $this->success('添加成功！', self::U('ad'));
                exit;
            } else {
                $this->error($model->getOneError() ? $model->getOneError() : '添加失败！');
            }
        }
    }

}
